package cn.edu.suda.schoolBus.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.suda.schoolBus.bean.SchoolBus;
import cn.edu.suda.schoolBus.bean.SchoolBusExample;
import cn.edu.suda.schoolBus.dao.SchoolBusMapper;
import cn.edu.suda.schoolBus.service.SchoolBusService;
import cn.edu.suda.schoolBus.util.ResultUtil;

@Service
public class SchoolBusServiceImpl implements SchoolBusService {

	@Autowired
	private SchoolBusMapper schoolBusDao;

	private SchoolBusExample schoolBusExample;

	@Override
	public int addSchoolBus(SchoolBus schoolBus) {
		schoolBus.setSbTime(new Date());
		return schoolBusDao.insertSelective(schoolBus);
	}

	@Override
	public Map<String, Object> getSchoolBus(int page, int rows) {
		int start = (page - 1) * rows;
		schoolBusExample = new SchoolBusExample();
		schoolBusExample.createCriteria().andSbIdIsNotNull();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", schoolBusDao.countByExample(schoolBusExample));
		map.put("rows", schoolBusDao.selectByPage(start, rows));
		return map;
	}

	@Override
	public int deleteByPrimaryKey(String ids) {
		schoolBusExample = new SchoolBusExample();
		schoolBusExample.createCriteria().andSbIdIn(ResultUtil.getResult(ids));
		return schoolBusDao.deleteByExample(schoolBusExample);
	}

	@Override
	public int editSchoolBus(SchoolBus schoolBus) {
		return schoolBusDao.updateByPrimaryKeySelective(schoolBus);
	}

	@Override
	public List<SchoolBus> getSchoolBus() {
		return schoolBusDao.selectDeparturePassengerNumber();
	}

	@Override
	public int getSchoolBus(String sbNumber) {
		schoolBusExample = new SchoolBusExample();
		schoolBusExample.createCriteria().andSbNumberEqualTo(sbNumber);
		return schoolBusDao.selectByExample(schoolBusExample).size();
	}

	@Override
	public List<String> getSchoolBusNumber() {
		return schoolBusDao.selectSbNumber();
	}

}
